

package com.trade.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;


import com.trade.model.Role;
import com.trade.model.Customer;
import com.trade.service.ICustomerService;

/**
 * 
 * @author dean.peng
 * 
 */
public class CustomerAction extends DispatchAction {
    private static Log log = LogFactory.getLog(Customer.class);
    private ICustomerService customerService;

    public ActionForward addCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

    	String strCustomerID = request.getParameter("CustomerID");
    	String strCustomerName = request.getParameter("CustomerName").trim();
        String strCustomerAddress = request.getParameter("CustomerAddress").trim();
        String strMobilenumber = request.getParameter("mobilenumber");
        strMobilenumber = null == strMobilenumber ? null : strMobilenumber.trim();

        
		Customer customeruser = new Customer();
        Boolean isSuccess = false;
        if(strCustomerID==null||strCustomerID=="")
        {
        	customeruser.setCustomerName(strCustomerName);
        	customeruser.setCustomerAddress(strCustomerAddress);
        	customeruser.setCustomerType(1);
        	customeruser.setMobileNumber(strMobilenumber);
	        isSuccess = customerService.AddCustomer(customeruser);
        }
        else
        {
        	customeruser = customerService.GetCustomerById(strCustomerID);
        	customeruser.setCustomerName(strCustomerName);
        	customeruser.setCustomerAddress(strCustomerAddress);
        	customeruser.setCustomerType(1);
        	customeruser.setMobileNumber(strMobilenumber);
	        isSuccess = customerService.UpdateCustomer(customeruser);
        }
       
        if (isSuccess) {
        	request.setAttribute("failCustomerName", "成功加入");
            return mapping.findForward("addcustomer");
        } else {
            request.setAttribute("CustomerName", strCustomerName);
            request.setAttribute("failCustomerName", "失败加入，用户名重复");
        	return mapping.findForward("addcustomer");
        }

    }
    
    public ActionForward GotoAddCustomers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	
    	request.setAttribute("failCustomerName", "提交信息提示");
        return mapping.findForward("addcustomer");

    }
    
    
    public ActionForward GetCustomers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        
        List<Customer> Customers = customerService.getAllCustomerUsers();
        request.setAttribute("customers",Customers);
        return mapping.findForward("customerlist");

    }
    
    public ActionForward DeleteCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

    	String strCustomerID = request.getParameter("CustomerID");
    	Boolean isSuccess = customerService.DeleteCustomer(strCustomerID);
    	if(!isSuccess)
    	{
    		return mapping.findForward("customerlist");
    	}
        List<Customer> Customers = customerService.getAllCustomerUsers();
        request.setAttribute("customer",Customers);
        return mapping.findForward("customerlist");

    }

    public ActionForward EditCustomer(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    	log.debug("edit Customer...");
        String strCustomerID = request.getParameter("CustomerID");
        Customer customer = customerService.GetCustomerById(strCustomerID);
        request.setAttribute("CustomerID", customer.getCustomerID());
        request.setAttribute("CustomerName", customer.getCustomerName());
        request.setAttribute("customerAddressvalue", customer.getCustomerAddress());
        request.setAttribute("mobilenumber", customer.getMobileNumber());
    	request.setAttribute("failCustomerName", "提交信息提示");
        return mapping.findForward("addcustomer");
    }

	

    public void setCustomerService(ICustomerService customerService) {
        this.customerService = customerService;
    }
    
    public ICustomerService getCustomerService()
    {
    	return this.customerService;
    }


    
}
